Skip to main content

Jest

JUnit XML serves as a standard format for integration between tools that deal with test results. BuildPulse reads test results using this format, and you can use it for your analysis as well. The files will have an xml file structure, each test case as a node, and file name extension will be xml.

In the steps below, we'll start with a JavaScript NodeJS project that has an existing Jest test suite (integration or unit tests), and we'll add JUnit XML as an additional output format for the test suite. In each step, we'll show the Git diff for the change that we're making.

  1. Add jest-junit as a development dependency.

    npm install --save-dev jest-junit

    Verify that your package.json file includes this new dependency:

       "author": "Richard Hendricks",
    "license": "MIT",
    "devDependencies": {
    - "jest": "^26.6.3"
    + "jest": "^26.6.3",
    + "jest-junit": "^12.0.0"
    }
  2. Update your jest.config.js file to enable the jest-junit reporter in addition to the default reporter as shown below. The diff below also configures various improvements to the JUnit XML output, like configuring output directory, output name, or including the file path for each test.

    module.exports = {
    - reporters: [ "default" ]
    + "reporters": [
    + "default",
    + ["jest-junit", {
    + addFileAttribute: "true",
    + ancestorSeparator: " › ",
    + classNameTemplate: "{classname}",
    + titleTemplate: "{title}", // outputname
    + }]
    + ]
    };

3. **By default, jest-junit writes the report to a file named junit.xml at the root of your project.**
Add this file to your .gitignore file so that it doesn't accidentally get checked into the repository.

```diff
@@ -1 +1,2 @@
+/junit.xml
/node_modules
Commit these changes to your repository.
  1. Commit these changes to your repository using the git CLI/command line tool.

    git commit -am "Update CI to generate JUnit XML for test results"

    The final result of these changes should resemble commit 674249d in the buildpulse-example-jest GitHub repository - or view the README for setup repository instructions.

    Whether your test runs on pull requests or period builds, you should now see generated junit xml files in your test reports directory.